-- Parsec
-- Module 'musplayer'

function sound_(snd)
  return function() 
    play_sound(snd);
  end
end

function music_(mus, loop, fadeout, fadein)
  return function() 
    play_music(mus, loop, fadeout, fadein);
  end
end

function mute_(fadeout,fadein)
  return function()
    play_music(nil, nil, fadeout, fadein);
  end
end

function playonce_(file,loop)
  return function()
    local hw = here();
    if hw["_"..file] == nil then
      play_music(file,loop);
      hw["_"..file] = true;
    end
  end
end

function play_music(file,loop,fadeout,fadein)
  local fl = file;
  local fout,fin = 2000,2000;
  if file ~= nil and file ~= "" then
    fl = "mus/"..file..".ogg";
  end
  if fadeout ~= nil then    
    fout = fadeout;
  end
  if fadein ~= nil then
    fin = fadein;
  end
  set_music_fading(fadeout, fadein);
  if loop == nil then
    loop = 1;
  end
  if fl ~= nil then
    set_music(fl, loop);
  else
    stop_music();
    mute_sound();
    _lastsound = nil;
  end
end

function play_sound(file,loop,ch)
  notnil(file,"file");
  local fl = "mus/"..file..".ogg";
  if ch == nil then
    ch = 2;
  end
  set_sound(fl, ch, loop);
  if loop ~= nil then
    _lastsound = {file,loop,ch};
  end
end

function mute_sound()
  stop_sound(2);
  stop_sound(3);
  stop_sound(4);
end